//
//  LiveHomeNavigatorView.swift
//  DYSwiftFramework
//
//  Created by huangdeyu on 2021/10/20.
//  Copyright © 2021 none. All rights reserved.
//

import UIKit
import SnapKit

class LiveHomeNavigatorView: UIView {
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var titles:[UIButton] = []
    private var btmLine = UIView.init()
    var avatarBtn = UIButton.init(type: .custom)
    var searchBtn = UIButton.init(type: .custom)
    // 手动触发回调
    var onTap:((Int,Int)->Void)?
    var currentIndex = 0
    
    init(titles:[String],visibleIndex:Int) {
        super.init(frame: CGRect.zero)
        currentIndex = visibleIndex
        backgroundColor = UIColor.white
        let stackView = UIStackView.init()
        stackView.spacing = 10
        stackView.alignment = .fill
        addSubview(stackView)
        stackView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(STATUS_BAR_HEIGHT)
            make.height.equalTo(NAV_HEIGHT - STATUS_BAR_HEIGHT)
        }
        
        for (index,title) in titles.enumerated() {
            let button = UIButton.init(type: .custom)
            button.setTitle(title, for: .normal)
            button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
            button.setTitleColor(Color.black, for: .selected)
            button.setTitleColor(Color.gray, for: .normal)
            if index == self.currentIndex {
                button.isSelected = true
            }
            self.titles.append(button)
            button.onTap = { [weak self] (btn) in
                if let action = self?.onTap {
                    action(self?.currentIndex ?? 0,index)
                }
            }
            
            stackView.addArrangedSubview(button)
        }
        btmLine.backgroundColor = Color.init(hex: 0xfa3c3c)
        // 这个需要先确定尺寸才能用
        btmLine.frame = CGRect.init(x: 0, y: 0, width: 16, height: 4)
        btmLine.roundCorners(.allCorners, radius: 2)
        addSubview(btmLine)
        btmLine.snp.makeConstraints { (make) in
            make.top.equalTo(self.titles[currentIndex].snp.bottom).offset(-10)
            make.centerX.equalTo(self.titles[currentIndex])
            make.height.equalTo(4)
            make.width.equalTo(16)
        }
        addSubview(avatarBtn)
        avatarBtn.setImage(UIImage.init(named: "live-home-nav-avatar"), for: .normal)
        avatarBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(STATUS_BAR_HEIGHT)
            make.width.equalTo(64)
            make.height.equalTo(NAV_HEIGHT - STATUS_BAR_HEIGHT)
        }
        
        searchBtn.setImage(UIImage.init(named: "live-home-nav-search"), for: .normal)
        addSubview(searchBtn)
        searchBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.top.equalToSuperview().offset(STATUS_BAR_HEIGHT)
            make.width.equalTo(64)
            make.height.equalTo(NAV_HEIGHT - STATUS_BAR_HEIGHT)
        }
        
        let divider = UIView.init()
        divider.backgroundColor = UIColor.init(hex: 0xf2f2f2)
        addSubview(divider)
        divider.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    func scrollTo(index:Int) {
        if self.currentIndex == index {
            return
        }
        self.titles.forEach({ (item) in
            item.isSelected = false
        })
        self.titles[index].isSelected = true
        self.currentIndex = index
        UIView.animate(withDuration: 0.3) {
            self.btmLine.snp.remakeConstraints { (make) in
                make.top.equalTo(self.titles[index].snp.bottom).offset(-10)
                make.centerX.equalTo(self.titles[index])
                make.height.equalTo(4)
                make.width.equalTo(16)
            }
            self.layoutIfNeeded()
        }
    }
    
}
